define(["../common"], function() {

	var $form = $("#regForm");
	var $msgWrap = $("#msgWrap");
	var checkNamePath = g_domain + "/member/checkName";
	var checkCaptchaPath = g_domain + "/captcha/check";
	var registerDoPath = g_domain + "/member/registerDo/json";
	var loginPath = g_domain + "/member/login";

	var formConfig = {
		fields: {
			"member.name": {
				rule: "required;username;remote[" + checkNamePath + "]",
				tip: "由6~16数字、字母、下划线组成"
			},
			"member.password": {
				rule: "required;password",
				tip: "由6~16数字、字母、特殊字符（_、^、!、#、%）组成"
			},
			"#password2": {
				rule: "required;match(member.password)",
				tip: "再次输入上面的密码",
				msg: {match: "两次输入的密码不一致"}
			},
			"company.name": {
				rule: "required;length[3~40]",
				tip: "由3~40个字符组成"
			},
			"company.contacts": {
				rule: "required;length[2~20]",
				tip: "由2~20个字符组成"
			},
			"company.phone": {
				rule: "required;phone",
				tip: "填写手机或者座机号"
			},
			"company.address": {
				rule: "required;length[3~80]",
				tip: "由3~80个字符组成"
			},
			captcha: {
				rule: "required;remote[" + checkCaptchaPath + "]",
				tip: "看不清？点击它换一张吧"
			}
		},
		valid: function(form){
			$msgWrap.tip("正在注册...", 'loading');
			var me = this;
			me.holdSubmit();
			$.post(registerDoPath, $form.serialize(), function(data){
					me.holdSubmit(false);
					if (data.status !== 0) {
						$msgWrap.tip(data.message, 'error', -1);
						$("#captchaImg").click();
						return;
					} 
					$msgWrap.tip("注册成功!...", 'loading', -1);
					setTimeout(function() {
						location.href = loginPath;
					},2000);
			});
		}
	}

	loadjs(["validator"], function() {
		$form.validator(formConfig);
		$("[name='type']:checked").click();
		
	});
	
	//表单样式
	$(".fill p").addClass("f_clear");
	$(".form input[type=text]").addClass("k");
	$(".form input[type=password]").addClass("k");
	$(".form input[type=radio]").addClass("radio");
	$(".form input[type=text],.form input[type=password]").focus(function(){
	      $(this).addClass("hover");
		  if($(this).val() === this.defaultValue){  
            //$(this).val(""); 
			  $(this).addClass("selected"); 
		  } 
	}).blur(function(){
		 $(this).removeClass("hover");
		 if ($(this).val() === '') {
          //$(this).val(this.defaultValue);
			$(this).removeClass("selected");
       }
	});
	 //

	$("[id^='submitBt_']").click(function() {
		$form.trigger('validate');
	});

	$("[name='type']").click(function() {
		// 要忽略企业信息表单验证
		var ignoreIds = "#companyInfo input";
		if ($(this).val() === "1") {
			$("#companyInfo").show();
			ignoreIds = null;
		} else {
			$("#companyInfo").hide();
		}
		$form.data('validator').options.ignore = ignoreIds;
	});

});
